package com.daocao;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author zxy
 */

//@Component
@Aspect
//ProceedingJoinPoint  可以执行拦截到方法的连接点对象
/**SystemMethodInterceptsLogger 配置切入点 */
public class SystemMethodInterceptsLogger {

    @Pointcut("execution(* com..*.*(..))")
    public void pointcut() {
    }




  @Around("pointcut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        Object result = "0-0";
        if (joinPoint == null || joinPoint.getSignature() == null || joinPoint.getTarget() == null || joinPoint.getArgs() == null) {
            return null;
        }

        String targetclass = joinPoint.getTarget().toString();

        String method_long = joinPoint.getSignature().toLongString();

        String method_short = joinPoint.getSignature().toShortString();


        System.out.println
                ("\n|-------------------------------环绕增强，拦截到已执行方法信息如下-------------------------------begin|"
                        + "\n|   类名： " + targetclass + "                                                                             |"
                        + "\n|   短方法：" + method_short
                        + "\n|   长方法：" + method_long
                        + "\n|"

                        + "\n|                                                                                              |"
                        + "\n|-------------------------------------------------------------------------------------------end|");
        System.out.println();

        joinPoint.proceed(joinPoint.getArgs());
        joinPoint.proceed();
return result;
    }
   // @Around("pointcut()")
    public Object around2(ProceedingJoinPoint joinPoint) {
        Object result = "0-0";
        if (joinPoint==null||joinPoint.getSignature()==null||joinPoint.getTarget()==null||joinPoint.getArgs()==null)
        {
            return null;
        }

        String targetclass = joinPoint.getTarget().toString();
        System.out.println(targetclass);
        String method_long = joinPoint.getSignature().toLongString();
        System.out.println(method_long);
        String method_short = joinPoint.getSignature().toShortString();
        System.out.println(method_long);
        Object[] args = joinPoint.getArgs();
        System.out.println(args);
        String argsType = "";
        if (null != args && args.length > 0)

            for (Object type : args)
                if (null != type)
                    argsType += type.getClass() + ",";
        String returnType = "";
        if (null != result && null != result.getClass()) {
            returnType = result.getClass() + "";
        }
        String args_s = Arrays.toString(args);
        try {
            if (joinPoint.proceed() != null ) {
                result = joinPoint.proceed();
            }

            System.out.println
                    ("\n|-------------------------------环绕增强，拦截到已执行方法信息如下-------------------------------begin|"
                            + "\n|   类名： " + targetclass + "                                                                             |"
                            + "\n|   短方法：" + method_short
                            + "\n|   长方法：" + method_long
                            + "\n|"
                            + "\n|   参数值：" + args_s.toString()
                            + "\n|   参数类型：" + args_s.getClass()
                            + "\n|"
                            + "\n|   返回值:  " + result.toString()
                            + "\n|   返回值类型:  " + result.getClass() + ""
                            + "\n|                                                                                              |"
                            + "\n|-------------------------------------------------------------------------------------------end|");
            System.out.println();


            System.out.println();
            return result;
        } catch (Throwable e) {
            System.out.println("");
            System.out.println(" 异常通知\n" + method_short + "方法执行抛异常了");
            System.out.println();
            System.out.println();
            e.printStackTrace();
        } finally {

        }
        return result;

    }

    //   @param joinPoint 连接点-- 拦截到的方法
 //   @Before("pointcut()")
    public void before(JoinPoint joinPoint) {
        /*被代理的对象*/

        /*拦截的类的名称*/

        System.out.println("拦截到的类名：" + joinPoint.getTarget().getClass().getName());
        /*获取方法对象*/
        Signature signature = joinPoint.getSignature();
        /*获取方法名*/
        String methodName = signature.getName();
        System.out.println("拦截到方法名：" + methodName);
        System.out.println(" ");




    }

    //@AfterReturning("pointcut()")
    public void afterReturning() {
        System.out.println("后置增强");
    }

    /*    @After("pointcut()")*/
    public void after() {
        System.out.println("最终增强");
    }

    /*    @AfterThrowing(value = "pointcut()",throwing = "e")*/
    public void afterThrowing(Exception e) {
        System.out.println("执行的方法的异常：" + e);
        System.out.println("异常通知");
    }
}
